package org.fuxia.im.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.entries.ECResponse;
import org.fuxia.im.core.utils.ObjectUtil;
import org.fuxia.im.db.dao.ChatGroupDaoImpl;
import org.springframework.stereotype.Service;

@Service(value="QUERY_GROUP")
public class QueryGroupService extends AbstractService {

	@Resource
	private ChatGroupDaoImpl chatGroupDao ;
	
	@SuppressWarnings("rawtypes")
	public void service(ECPacket packet) {
		String queryField = packet.getParameter("queryField") ;
		String userCode =packet.getParameter("userCode") ;
		String condtion = packet.getParameter("condition") ;
		String sql = "select cg.id,cg.groupName,cg.ownerId from ChatGroup cg " +  
					 "where  cg.%s like :condition and not exists (select ucg.chatGroupId from UserChatGroup ucg where " +
					 " ucg.userCode=:userCode and ucg.chatGroupId=cg.id)" ;
		ECResponse response = new ECResponse() ;
		response.setResponseType("QUERY_GROUP_RESP") ;
		try{
			List result = chatGroupDao.findByNamedParam(String.format(sql, queryField), new String[]{
				  "userCode","condition"
				}, new Object[]{userCode,"%" + condtion+"%"}) ;
			response.put("result", ObjectUtil.getJsonStr(result)) ;
			response.setStatusCode(200);
		}catch (Exception e) {
			e.printStackTrace();
			response.setStatusCode(400);
			response.setResponseText("查询数据失败") ;
		}
		
		writeReponse(packet, response) ;
		
		
	}
	

}
